import { Map } from 'ol'
import { LineString } from 'ol/geom'
import { LOCATION } from '../../Utils/PlotTypes'

class Location extends LineString {
  constructor (coordinates, points, params) {
    super([])
    this.type = LOCATION
    this.freehand = false
    this.fixPointCount = 3
    this.set('params', params)
    this.devLength = 150
    if (params.devLength) {
      this.devLength = params.devLength
    }
    if (points && points.length > 0) {
      this.setPoints(points)
    } else if (coordinates && coordinates.length > 0) {
      this.setCoordinates(coordinates)
    }
  }

  /**
   * 获取标绘类型
   * @returns {*}
   */
  getPlotType () {
    return this.type
  }

  /**
   * 执行动作
   */
  generate () {
    this.setCoordinates(this.points)
  }

  /**
   * 设置地图对象
   * @param map
   */
  setMap (map) {
    if (map && map instanceof Map) {
      this.map = map
    } else {
      throw new Error('传入的不是地图对象！')
    }
  }

  /**
   * 获取当前地图对象
   * @returns {ol.Map|*}
   */
  getMap () {
    return this.map
  }

  /**
   * 判断是否是Plot
   * @returns {boolean}
   */
  isPlot () {
    return true
  }

  /**
   * 设置坐标点
   * @param value
   * @param range 范围参数
   */
  setPoints (value) {
    if (value.length !== 3) {
      var change = this.devLength
      var path = value[value.length - 1]
      var pointW = []; var pointE = []
      path.forEach((e, i) => { // 根据点击的坐标，得到点击坐标 固定距离的东和西坐标
        if (i === 0) {
          pointW.push(e - change)
          pointE.push(e + change)
        } else {
          pointW.push(e)
          pointE.push(e)
        }
      })
      value = [
        pointW, path, pointE
      ]
    }

    this.points = !value ? [] : value
    if (this.points.length >= 1) {
      this.generate()
    }
  }

  /**
   * 获取坐标点
   * @returns {Array.<T>}
   */
  getPoints () {
    return this.points.slice(0)
  }

  /**
   * 获取点数量
   * @returns {Number}
   */
  getPointCount () {
    return this.points.length
  }

  /**
   * 更新当前坐标
   * @param point
   * @param index
   */
  updatePoint (point, index) {
    if (index >= 0 && index < this.points.length) {
      this.points[index] = point
      this.generate()
    }
  }

  /**
   * 更新最后一个坐标
   * @param point
   */
  updateLastPoint (point) {
    this.updatePoint(point, this.points.length - 1)
  }

  /**
   * 结束绘制
   */
  finishDrawing () {
  }
}

export default Location
